package com.lagou.assignment;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * 定义数据bean
 * 进行排序和统计排名数据
 */
public class DataBean implements WritableComparable<DataBean> {

    /**
     * 排名数据
     */
    private int ranking;

    /**
     * 实际数值
     */
    private int data;

    public DataBean() {

    }

    public DataBean(int ranking, int data) {
        this.ranking = ranking;
        this.data = data;
    }

    /**
     * 进行数据排名比较
     * 0 ==
     * 1 <
     * -1 >
     *
     * @param o
     * @return
     */
    @Override
    public int compareTo(DataBean o) {
        return Integer.compare(this.getData(), o.getData());
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeInt(ranking);
        dataOutput.writeInt(data);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.ranking = dataInput.readInt();
        this.data = dataInput.readInt();
    }

    public int getRanking() {
        return ranking;
    }

    public void setRanking(int ranking) {
        this.ranking = ranking;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }

    /**
     * 写入文件的格式定义
     * 第一列为排名
     * 第二列为数值
     *
     * @return
     */
    @Override
    public String toString() {
        return ranking + "\t" + data;
    }
}
